package top.glkj.hms.controller.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.glkj.hms.entity.User;
import top.glkj.hms.exception.DefineException;
import top.glkj.hms.service.UserService;
import top.glkj.hms.utils.Result;
import top.glkj.hms.utils.ResultFactory;

import java.util.List;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/11/29 14:45
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/system/user")
public class UserManagementController {
    private final UserService userService;

    @Autowired
    public UserManagementController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation("添加用户")
    @PostMapping
    public Result addUser(@RequestBody User user) {
        //System.out.println(Arrays.toString(roles.toArray()));
        System.out.println(user);
        return userService.addUser(user);
    }

    @ApiOperation("添加用户")
    @PostMapping("/addAll")
    public Result addAllUser(List<User> user) throws DefineException {
        throw new DefineException("no implement");
    }

    @ApiOperation("删除用户")
    @DeleteMapping
    public Result removeUser(Long id) {
        return userService.remove(new User(id));
    }

    @ApiOperation("获取用户")
    @GetMapping("/{id}")
    public Result getUser(@PathVariable(name = "id") Long id) {
        return userService.getOne(id);
    }

    @ApiOperation("获取所有用户")
    @GetMapping
    public Result getAllUser() {
        return userService.getAll();
    }

    @ApiOperation("分页获取所有用户")
    @GetMapping("/{size}/{page}")
    public Result getAllUser(@PathVariable(name = "size") int size, @PathVariable(name = "page") int page) {
        return userService.getAllUserByPageAndSize(page, size);
    }
    @ApiOperation("分页获取所有用户,根据角色id")
    @GetMapping("/{rid}/{size}/{page}")
    public Result getAllUserByRoleId(@PathVariable(name = "size") int size, @PathVariable(name = "page") int page,@PathVariable(name = "rid") Long rid) {
        return userService.getAllUserByPageAndSize(page, size);
    }
    @ApiOperation("更新密码")
    @PutMapping("/updatePassWord")
    public Result updatePassWord(Long id, String password) {

        return userService.updatePassWord(id, password);
    }

    @ApiOperation("批量更新密码")
    @PutMapping("/updateAllPassWord")
    public Result updateAllPassWord(List<Long> ids, String password) {
        try {
            return userService.updateAllPassWord(ids, password);
        } catch (DefineException e) {
            return ResultFactory.isError(e.getMessage());
        }
    }

    @ApiOperation("修改用户")
    @PutMapping
    public Result updateUser(User user, List<String> fields) {
        return userService.updateUser(user, fields);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/delete")
    public Result deleteUser(Long id) {
        return userService.delete(new User(id));
    }

    @ApiOperation("用户添加角色")
    @PostMapping("/role")
    public Result userAddRole(Long uid, List<Long> rids) {
        try {
            return userService.userAddRole(uid, rids);
        } catch (DefineException e) {
            return ResultFactory.isError(e.getMessage());
        }
    }

    @ApiOperation("用户修改角色")
    @PutMapping("/role")
    public Result userUpdateRole(Long uid, List<Long> rids) {
        try {
            return userService.userUpdateRole(uid, rids);
        } catch (DefineException e) {
            return ResultFactory.isError(e.getMessage());
        }
    }
}
